package org.itboys.mobile.controller.web.selling;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.TianJieGroup;
import org.itboys.mobile.entity.mysql.selling.TianJieSellingReward;
import org.itboys.mobile.service.mysql.web.TianJieGroupService;
import org.itboys.mobile.service.mysql.web.selling.TianJieSellingRewardService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_金融_直销_奖励（等级或星级）_controller_web
 */
@Controller
@RequestMapping("/tianjie/web/finance/selling/reward")
public class TianJieSellingRewardController extends BaseController {
    @Autowired
    private TianJieSellingRewardService sellingRewardService;

    /**
     * 主页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model,
                        @RequestParam(value = "type", required = true) Long type) {
        String url = "/tianjie/selling/reward";
        //等级
        if (type == 1) {
            url += "/grade/index";
            //星级经理
        } else if (type == 2) {
            url += "/star/index";
        }

        return url;
    }


    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value = "type", required = true) Integer type) {
        try {
            PageResult<TianJieSellingReward> result = sellingRewardService.selectReward(request, type);
            JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }


    /**
     * 单个查询
     *
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/findOne")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "id", required = true) Long id,
            @RequestParam(value = "type", required = false) Integer type) {
        try {
            TianJieSellingReward sellingReward = sellingRewardService.findOne(id, type);
            ResponseMessage.success(sellingReward);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/selling/reward/findOne------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);

        }
    }

    /**
     * 新增或者修改团队
     *
     * @param request
     * @param response
     * @param reward
     */
    @RequestMapping("/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute TianJieSellingReward reward) {
        try {
            sellingRewardService.saveReward(reward);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/selling/reward/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);
        }
    }
}
